package com.netease.xone.fragment;

import java.util.ArrayList;
import java.util.List;

import protocol.meta.Subject;
import Utils.StackTraceUtil;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.widget.CursorAdapter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.adapter.AdapterTip;
import com.netease.xone.itemview.ItemViewTipBase;

import db.manager.ManagerDiscussTip;


public class FragmentTipSubject extends FragmentTipBase {
    private static final String TAG = FragmentTipSubject.class.getSimpleName();
    
    public static FragmentTipSubject newInstance(Parcelable obj) {
        FragmentTipSubject f = new FragmentTipSubject();
        Bundle b = new Bundle();
        b.putParcelable("obj", obj);
        f.setArguments(b);
        return f;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        NTLog.d(TAG, StackTraceUtil.getMethodName()
                + " savedInstanceState=" + (null == savedInstanceState ? "null" : savedInstanceState.toString()));
        super.onCreate(savedInstanceState);
        mType = ItemViewTipBase.TYPE_SUBJECT;
        getSherlockActivity().getSupportActionBar().setTitle(R.string.discuss_title_subject);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (null != mLetterIndexView) {
            mLetterIndexView.setVisibility(View.VISIBLE);
        }
        return view;
    }
    
    @Override
    protected CursorAdapter getAdapter() {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        AdapterTip adapter = null;
        adapter = new AdapterTip(getActivity(), null, ItemViewTipBase.TYPE_SUBJECT);
        adapter.setAlphabetIndexer(mAlphaIndex);
        return adapter;
    }

    @Override
    protected String getItemInfo(Object obj, int position) {
        String value = null;
        List<Subject> list = null;
        list = new ArrayList<Subject>();
        do {
            if (null == obj) {
                break;
            }
            if (obj instanceof Subject) {
                if (!TextUtils.isDigitsOnly(((Subject)obj).subjectName.substring(0, 1))) {
                    list.add((Subject)obj);
                    ManagerDiscussTip.replaceSubject(list, true);
                }
                value = "*" + ((Subject)obj).subjectName + "* ";
            }
        } while (false);
        return value;
    }
}
